package com.br.unisal.library.view.graphic;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.br.unisal.library.view.LibraryOperations;
import com.br.unisal.library.view.LibraryOperationsInterface;
import com.br.unisal.library.view.ScreenFactory;
import com.unisal.library.util.ScreenType;

public class LoginGraphicView {

	private JFrame frmBibliotecaUnisal;
	private final JLabel lblLogin = new JLabel("Login");
	private final JLabel lblUsurio = new JLabel("Usu\u00E1rio");
	private final JTextField tfUsuario = new JTextField();
	private final JLabel lblSenha = new JLabel("Senha");
	private final JTextField tfSenha = new JTextField();
	private final JButton btnEntrar = new JButton("Entrar");
	private LibraryOperationsInterface operations = new LibraryOperations();
	private ScreenFactory screenFactory;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginGraphicView window = new LoginGraphicView();
					window.frmBibliotecaUnisal.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LoginGraphicView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		tfSenha.setColumns(10);
		tfUsuario.setColumns(10);
		lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 18));
		frmBibliotecaUnisal = new JFrame();
		frmBibliotecaUnisal.setTitle("Biblioteca Unisal");
		frmBibliotecaUnisal.setBounds(100, 100, 450, 300);
		frmBibliotecaUnisal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GroupLayout groupLayout = new GroupLayout(frmBibliotecaUnisal.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblLogin, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(tfSenha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblSenha))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(tfUsuario, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(lblUsurio)))
						.addComponent(btnEntrar))
					.addContainerGap(298, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblLogin, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblUsurio)
						.addComponent(tfUsuario, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSenha)
						.addComponent(tfSenha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(57)
					.addComponent(btnEntrar)
					.addContainerGap(57, Short.MAX_VALUE))
		);
		
		btnEntrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//TODO Implement completely
				String user = tfUsuario.getText();
				String password = tfSenha.getText();
				validateLogin(user, password);
				
			}
		});
		
		frmBibliotecaUnisal.getContentPane().setLayout(groupLayout);
	}
	
	private void validateLogin(String user, String password) {
		if(this.operations.login(user, password)) {
			screenFactory = new ScreenFactory(ScreenType.MAIN_MENU);
			screenFactory.createScreen();
			} else {
			//TODO implement
		}
	}
}
